package com.ecommerce.onion.application.cli;

import com.ecommerce.onion.domain.Product;
import com.ecommerce.onion.domain.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.UUID;
/**
 * @packageName: application.cli（应用.客户端包）
 * @className: CliOrderController(客户端订单控制器)
 * @description: 围绕此订单领域展开业务
 * @author: luds
 * @version: v1.0
 * @date: 2021-03/25
 */
@Component
public class CliOrderController {

    private static final Logger LOG = LoggerFactory.getLogger(CliOrderController.class);
    /**
     * 订单服务
     */
    private final OrderService orderService;
    /**
     * 由订单服务自动装配订单控制器
     * @param orderService
     */
    @Autowired
    public CliOrderController(OrderService orderService) {
        this.orderService = orderService;
    }
    /**
     * 创建完成订单
     */
    public void createCompleteOrder() {
        LOG.info("<<Create complete order>>");
        UUID orderId = createOrder();
        orderService.completeOrder(orderId);
    }

    /**
     * 创建不完整的订单
     */
    public void createIncompleteOrder() {
        LOG.info("<<Create incomplete order>>");
        UUID orderId = createOrder();
    }

    /**
     * 创建订单
     * @return 返回订单ID
     */
    private UUID createOrder() {
        LOG.info("Placing a new order with two products");
        Product mobilePhone = new Product(UUID.randomUUID(), BigDecimal.valueOf(200), "mobile");
        Product razor = new Product(UUID.randomUUID(), BigDecimal.valueOf(50), "razor");
        LOG.info("Creating order with mobile phone");
        UUID orderId = orderService.createOrder(mobilePhone);
        LOG.info("Adding a razor to the order");
        orderService.addProduct(orderId, razor);
        return orderId;
    }
}
